/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import local.PublicUserFacadeLocal;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Attempt;
import model.PublicUser;

/**
 *
 * @author Ken
 */
@Stateless
public class PublicUserFacade implements PublicUserFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public PublicUser create(PublicUser a) {
        em.persist(a);
        
        //@ken modified to create a return type
        return a;
    }

    @Override
    public void edit(PublicUser a) {
        em.merge(a);
    }

    @Override
    public void refresh(PublicUser a) {
        em.refresh(a);
    }

    @Override
    public String registration(String email, PublicUser a) {
        //refresh
        em.merge(a);
        
        String queryStr0 = "SELECT p FROM PublicUser p WHERE p.email=?1";
        Query query0 = em.createQuery(queryStr0);
        query0.setParameter(1, a.getEmail());
        
        ArrayList<PublicUser> aList = new ArrayList((Collection<PublicUser>) query0.getResultList());
        a = aList.get(0);

        //look for same email
        String queryStr = "SELECT p FROM PublicUser p WHERE p.email=?1";
        Query query = em.createQuery(queryStr);
        query.setParameter(1, email);
        
        ArrayList<PublicUser> pList = new ArrayList((Collection<PublicUser>) query.getResultList());

        if (pList.size() > 0) {
            PublicUser pu = pList.get(0);
            Collection<Attempt> attCol = a.getAttempt();
            a.setAttempt(null);
            pu.setName(a.getName());
            pu.setRole(a.getRole());

            em.merge(pu);
            //a.setIndustry(null);
            for (Attempt att1 : attCol) {
                //@ken modified to change all attempt public user fk
                att1.setPu_id(pu.getPub_user_id());
               
                pu.getAttempt().add(att1);
            }
            em.merge(pu);
            em.remove(a);
        } else {
            a.setEmail(email);
            em.merge(a);
        }
        
        return "success";
    }

    @Override
    public String retrieveReport(String email) {
        PublicUser a = new PublicUser();

        String queryStr0 = "SELECT p FROM PublicUser p "
                + "WHERE "
                + "p.email=?1";
        Query query0 = em.createQuery(queryStr0);
        query0.setParameter(1, email);
        query0.getResultList();


        if (query0.getResultList().isEmpty()) {
            return "fail";
        } else {
            return "success";
        }



    }
    @Override
    public String checkEmail(String email) {
        PublicUser a = new PublicUser();

        String queryStr0 = "SELECT p FROM PublicUser p "
                + "WHERE "
                + "p.email=?1";
        Query query0 = em.createQuery(queryStr0);
        query0.setParameter(1, email);
        query0.getResultList();


        if (query0.getResultList().isEmpty()) {
            return "fail";
        } else {
            return "success";
        }
    }
    
    @Override
    public PublicUser getPublicUser(String email){
        String queryStr0 = "SELECT p FROM PublicUser p "
                + "WHERE "
                + "p.email=?1";
        Query query0 = em.createQuery(queryStr0);
        query0.setParameter(1, email);
        query0.getResultList();
        
        if(query0.getResultList().size()==1){
            PublicUser pu = (PublicUser)query0.getSingleResult();
            return pu;
        }else{
            return null;
        }
    }
    
    @Override
    public PublicUser publicUserDetail(Long pu_id){
        PublicUser pu = em.find(PublicUser.class, pu_id);
        
        return pu;
    }
    
    @Override
    public ArrayList<PublicUser> getAllPublicUser(){
        
        String queryStr = "SELECT p FROM PublicUser p";
        Query query = em.createQuery(queryStr);
        
        ArrayList<PublicUser> pList = new ArrayList((Collection<PublicUser>) query.getResultList());
        return pList;
    }
    
    
}
